<template>
  <el-row class="center-text" justify="center">
    <el-col :offset="8" :span="8">
      <h1>注册</h1>
    </el-col>
    <el-col :span="8">
    </el-col>
  </el-row>
  <el-row>
    <el-col :offset="9" :span="6">
      <el-form :model="registerForm">
        <el-form-item label="邮箱">
          <el-input v-model="registerForm.email"/>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="registerForm.nickname"/>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="registerForm.password" type="password"/>
        </el-form-item>
        <el-form-item label="重复密码">
          <el-input v-model="registerForm.rePassword" type="password"/>
        </el-form-item>
        <el-form-item label="验证码">
          <el-input v-model="registerForm.verifyCode">
            <template #append>
              <el-button :disabled="verifyCodeButtonDisable" @click="getVerifyCode">{{ verifyCodeMsg }}</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">Create</el-button>
          <router-link to="/login">
            <el-button link>已有账号，去登录</el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script lang="ts" setup>
import {reactive, ref, watch} from "vue";
import {ElMessage} from "element-plus";
import {type RegisterDTO, type ResponseInterface} from "@/type/models";
import {getAPI, mockServiceURL} from "@/api/MyAxiosAPI";
import router from "@/router";
// 注册表单
let registerForm = reactive<RegisterDTO>({
  nickname: "",
  email: "",
  password: "",
  rePassword: "",
  verifyCode: ""
})
// axios
let isMock = false
let API = getAPI(isMock)
//--- 验证码
let verifyCodeMsg = ref("接收验证码")
// 验证码倒计时
const RE_GET_VERIFY_CODE_TIME = 60
let verifyCodeCD = ref(RE_GET_VERIFY_CODE_TIME)
let verifyCodeTimer: any = undefined
let verifyCodeButtonDisable = ref(false)

// 点击验证码按钮
function getVerifyCode() {
  // 检验email是否符合规则
  if (registerForm.email === "") {
    ElMessage.error("这不是一个有效的邮箱地址")
    return;
  }
  // 禁用按钮
  verifyCodeButtonDisable.value = true
  // 改变按钮文本为倒计时
  verifyCodeMsg.value = (verifyCodeCD.value--) + "秒后重新接受验证码"
  verifyCodeTimer = setInterval(() => {
    verifyCodeMsg.value = verifyCodeCD.value + "秒后重新接受验证码"
    verifyCodeCD.value--
  }, 1000)
  // 发送获取验证码请求
  API({
    url: mockServiceURL(isMock, "verify-code", "/verify-code/send"),
    method: "post",
    data: {
      "user": registerForm.email,
      "serviceName": "user-service"
    },
    timeout: 10000,
  })
}

// 验证码计时结束
watch(verifyCodeCD, (n) => {
  if (n <= -1) {
    clearInterval(verifyCodeTimer)
    verifyCodeButtonDisable.value = false
    verifyCodeMsg.value = "接受验证码"
    verifyCodeCD.value = RE_GET_VERIFY_CODE_TIME
  }
})
//---end 验证码

//--- 注册
function onSubmit() {
  API({
    url: mockServiceURL(isMock, "user", "/user/register"),
    method: "post",
    data: registerForm
  }).then((res: any) => {
    let data: ResponseInterface = res.data;
    if (data.code > 0) {
      ElMessage.success("注册成功，即将跳转到登录页面")
      let routeTimer = setTimeout(() => {
        router.push("/login")
        clearTimeout(routeTimer)
      }, 1000)
    }
  }).catch((err) => {
    ElMessage.error("注册失败")
  })
}

//--- end 注册
</script>
<style scoped>
.center-text {
  text-align: center;
}
</style>